//
//  IGPopPictureManager.m
//  jingdongfang
//
//  Created by BOE on 2018/4/10.
//  Copyright © 2018年 BOE-SBG-CTO. All rights reserved.
//

#import "IGPopPictureManager.h"
#import "IGADView.h"

#define KPOPPICTUREKEY [NSString stringWithFormat:@"pop%@", [IGUser getUid]]
#define KPOPPICTUREDATE @"popPictureDate"

@implementation IGPopPictureApi

- (NSString *)requestUrl {
    return @"homepage/popPicture.action";
}

@end

@implementation IGPopPicture

+ (JSONKeyMapper *)keyMapper
{
    return [[JSONKeyMapper alloc]
            initWithModelToJSONDictionary:@{
                                            @"pId":@"id"
                                            }];
}

@end

@implementation IGPopPictureModel

@end

@implementation IGPopPictureResp

@end

@implementation IGPopPictureManager

+ (void)popPicture {
    IGPopPictureApi *api = [IGPopPictureApi new];
    [api startWithDecodeClass:[IGPopPictureResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            IGPopPictureResp *resp = responseObject;
            IGPopPicture *popModel = resp.data.popPicture;
            if (!popModel.pId) {
                return;
            }
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            NSDate *now = [NSDate date];
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd"];
            [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
            NSString *agoDateString = [defaults objectForKey:KPOPPICTUREDATE];
            NSString *nowDateString = [dateFormatter stringFromDate:now];
            
            NSDate *agoDate = [dateFormatter dateFromString:agoDateString];
            
            IGADView *view = [[IGADView alloc] initWithModel:popModel];
            if ([popModel.validType integerValue] == 1) { //每次启动显示
                [view showInView:kKeyWindow];
                [defaults setValue:popModel.pId forKey:KPOPPICTUREKEY];
                [defaults synchronize];
            } else if (([popModel.validType integerValue] == 2)
                       && (![agoDateString isEqualToString:nowDateString]
                           || ![popModel.pId isEqualToString:[defaults valueForKey:KPOPPICTUREKEY]]))
            { //每日显示一次
                [view showInView:kKeyWindow];
                [defaults setValue:nowDateString forKey:KPOPPICTUREDATE];
                [defaults setValue:popModel.pId forKey:KPOPPICTUREKEY];
                [defaults synchronize];
            } else if (([popModel.validType integerValue] == 3)
                       && (![NSDate isSameWeekWithDate:agoDate]
                           || ![popModel.pId isEqualToString:[defaults valueForKey:KPOPPICTUREKEY]]))
            { //每周显示一次
                [view showInView:kKeyWindow];
                [defaults setValue:nowDateString forKey:KPOPPICTUREDATE];
                [defaults setValue:popModel.pId forKey:KPOPPICTUREKEY];
                [defaults synchronize];
            }
        }
        
    }];
}



@end

